/**
 * 异步请求验证用户名是否存在
 */
function comfirmUsername() {
    var $username = $("input[name=userName]").val()!=null?$("input[name=userName]").val():"";
    if ($username!="") {
        $.ajax({
            type: "POST",
            url: "user/confirmUsername",
            data: "userName=" + $username,
            dataType: "text",
            success: function (res) {
                if(res=="fail") {
                    $(".msg:eq(0)").text("账号已被注册");
                    return false;
                }else {
                    $(".msg:eq(0)").text("");
                    return true;
                }
            }
        });
    }
}

/**
 * 异步请求验证初始密码是否输入正确
 */
function comfirmInitPwd() {
    var $initPwd = $("input[name=initPassword]").val()!=null?$("input[name=initPassword]").val():"";

    if ($initPwd!="") {
        $.ajax({
            type: "POST",
            url: "confirminitPwd",
            data: "initPwd=" + $initPwd,
            dataType: "text",
            success: function (res) {
                if(res=="fail") {
                    $(".msg:eq(0)").text("原密码输入不正确");
                }else {
                    $(".msg:eq(0)").text("");
                }
            }
        });
    }
}

/**
 * 验证密码规则
 */
function comfirm() {
    var $comfirmPwd = $("input[name=confirmPassword]").val()!=null ? $("input[name=confirmPassword]").val():"";
    var $initPwd = $("input[name=initPassword]").val()!=null ? $("input[name=initPassword]").val():"";
    var $newPwd = $("input[name=newPassword]").val()!=null ? $("input[name=newPassword]").val():"";

    if ($initPwd!="" && $newPwd!="" && $initPwd == $newPwd) {
        $(".msg:eq(2)").text("新密码与原密码一致");
        return false;
    } else if ($comfirmPwd!="" && $newPwd!="" && $newPwd != $comfirmPwd) {
        $(".msg:eq(3)").text("两次输入密码不一致");
        return false;
    } else {
        $(".msg").text("");
        return true;
    }

}